import { onBeforeMount, onMounted, onBeforeUnmount } from "vue";
import { eventBus } from "@visual/utils";
//所有引入的组件都会执行这个方法
const useCommentEvent = () => {
  //订阅事件
  onBeforeMount(() => {
    //所有监听的事件都会执行，这是一个全量的广播，放到这里和写到组件内部的区别是啥

    eventBus.on("compClick", compClick);

    eventBus.on("compFilter", compFilter);
    eventBus.on("compRouter", compRouter);
    eventBus.on("compDialog", compDialog);

    console.log("onBeforeMount");
  });
  //操作dom相关
  onMounted(() => {
    console.log("onMounted");
  });
  //解绑事件
  onBeforeUnmount(() => {
    eventBus.off("compClick", compClick);
    eventBus.off("compFilter", compFilter);
    eventBus.off("compRouter", compRouter);
    eventBus.off("compDialog", compDialog);
    console.log("onBeforeUnmount");
  });
  //事件
  const compClick = ({ comp, params }) => {
    console.log("compClick", comp, params);
  };
  const compFilter = ({ comp, params }) => {
    console.log("compFilter", comp, params);
  };
  const compRouter = ({ comp, params }) => {
    console.log("compRouter", comp, params);
  };
  const compDialog = ({ comp, params }) => {
    console.log("compDialog", comp, params);
  };
  // const emitFilter = ({ comp, params }) => {
  //   eventBus.emit("compFilter", { comp, params });
  // }
  const unEmiteEvents = eventName => {
    switch (eventName) {
      case "compClick":
        eventBus.off("compClick", compClick);
        break;
      case "compFilter":
        eventBus.off("compFilter", compFilter);
        break;
      case "compRouter":
        eventBus.off("compRouter", compRouter);
        break;
      case "compDialog":
        eventBus.off("compDialog", compDialog);
        break;
    }
  };
  return {
    compClick,
    compFilter,
    compRouter,
    compDialog,
    // emitFilter,
    unEmiteEvents
  };
};
export default useCommentEvent;
